package com.hxkj.controller;

import com.hxkj.domain.po.Site;
import com.hxkj.service.AuthSiteService;
import com.platform.common.dispatcher.util.ResultStructUtils;
import com.platform.common.domain.exception.CustomMessageException;
import com.platform.common.domain.result.ParameterDataStruct;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.domain.result.ResultPageStruct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.Callable;

/**
 * 站点控制层
 * <p>
 * Created by zzx on 2018/5/15.
 */
@RestController
@RequestMapping("/site")
public class SiteController {

    private static final Logger LOGGER = LoggerFactory.getLogger(SiteController.class);

    @Autowired
    private AuthSiteService siteService;


    /**
     * 获取站点列表
     *
     * @param parameterDataStruct 站点信息 和 分页信息
     * @return 翻页数据
     * @throws Exception
     */
    @RequestMapping(value = "/getlist", method = RequestMethod.POST, consumes = "application/json;charset=UTF-8")
    public Callable<ResultPageStruct<Site>> getList(@RequestBody ParameterDataStruct<Site> parameterDataStruct) throws Exception {
        return () -> ResultStructUtils.buildResultPageStruct(siteService.getSiteList(parameterDataStruct.getData() == null ?
                new Site() : parameterDataStruct.getData(), parameterDataStruct.getPageTo()), "param=value");
    }

    /**
     * 获取站点切换列表
     *
     * @param
     * @return 翻页数据
     * @throws Exception
     */
    @RequestMapping(value = "/getswitcherlist", method = RequestMethod.POST, consumes = "application/json;charset=UTF-8")
    public Callable<ResultDataStruct<List<Site>>> getSwitcherList(@RequestBody Site site) throws Exception {
        return () -> siteService.getSwitcherList(site);
    }


    /**
     * 获取单一用户
     *
     * @param site
     * @return 用户信息
     * @throws Exception
     */
    @RequestMapping(value = "/get", method = RequestMethod.POST, consumes = "application/json;charset=UTF-8")
    public Callable<ResultDataStruct<Site>> getSite(@RequestBody Site site) throws Exception {
        return () -> siteService.getSiteFromCache(site);
    }

    /**
     * 新增站点
     *
     * @param site 站点信息
     * @throws Exception
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST, consumes = "application/json;charset=UTF-8")
    public Callable<ResultMessageStruct> saveSite(@RequestBody Site site) throws Exception {
        return () -> siteService.saveSite(site);
    }

    /**
     * 删除站点
     *
     * @param site 站点
     * @throws Exception
     */
    @RequestMapping(value = "/remove", method = RequestMethod.POST, consumes = "application/json;charset=UTF-8")
    public Callable<ResultMessageStruct> deleteSite(@RequestBody Site site) throws Exception {
        return () -> siteService.deleteSite(site);
    }

    /**
     * 修改站点
     *
     * @param site 站点信息
     * @throws Exception
     */
    @RequestMapping(value = "/modify", method = RequestMethod.POST, consumes = "application/json;charset=UTF-8")
    public ResultMessageStruct updateSite(@RequestBody Site site) {
            ResultMessageStruct resultMessageStruct = new ResultMessageStruct();
            try {
                siteService.updateSite(site);
                resultMessageStruct.setCode(200);
                resultMessageStruct.setMessage("成功");
            } catch (CustomMessageException e) {
                LOGGER.error("修改站点失败:{}", e.getMessage());
                resultMessageStruct.setCode(e.getCode());
                resultMessageStruct.setMessage(e.getMessage());
            } catch (Exception e) {
                e.printStackTrace();
                LOGGER.error("修改站点失败:{}", e.getMessage());
                resultMessageStruct.setCode(400);
                resultMessageStruct.setMessage(e.getMessage());
            }
        return resultMessageStruct;

    }

    /**
     * 站点排序
     *
     * @param site 站点id 数组信息
     * @throws Exception
     */
    @RequestMapping(value = "/sort", method = RequestMethod.POST, consumes = "application/json;charset=UTF-8")
    public Callable<ResultMessageStruct> SiteSort(@RequestBody Site site) throws Exception {
        return () -> siteService.SiteSort(site);
    }

    @RequestMapping(value = "/getAllSite")
    public Callable<List<Site>> getAllSite(@RequestBody Site site) throws Exception{
        return () -> siteService.getList(site);
    }

    @RequestMapping(value = "/getSiteLogo")
    public Callable<String> getSiteLogo(@RequestBody Long siteId) throws Exception{
        return () -> siteService.getSiteLogo(siteId);
    }

    /**
     * 获取站点列表，包含站点类型信息
     *
     * @param parameterDataStruct 站点信息 和 分页信息
     * @return 翻页数据
     * @throws Exception
     */
    @RequestMapping(value = "/getsiteinfolist", method = RequestMethod.POST, consumes = "application/json;charset=UTF-8")
    public Callable<List<Site>> getSiteInfoList(@RequestBody ParameterDataStruct<Site> parameterDataStruct) throws Exception {
        return () -> siteService.getSiteInfoList(
                parameterDataStruct.getData() == null ? new Site() : parameterDataStruct.getData(),
                parameterDataStruct.getPageTo());
    }
}
